package com.neu.carbon.task.controller;

import java.util.List;

import com.neu.common.core.domain.model.LoginUser;
import com.neu.common.utils.ServletUtils;
import com.neu.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.neu.common.annotation.Log;
import com.neu.common.core.controller.BaseController;
import com.neu.common.core.domain.AjaxResult;
import com.neu.common.enums.BusinessType;
import com.neu.carbon.task.domain.Tasklist;
import com.neu.carbon.task.service.ITasklistService;
import com.neu.common.utils.poi.ExcelUtil;
import com.neu.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 任务列表Controller
 * 
 * @author neuedu
 * @date 2025-08-25
 */
@Api(tags = {"任务列表"})
@RestController
@RequestMapping("/task/task")
public class TasklistController extends BaseController
{
    @Autowired
    private ITasklistService tasklistService;

    @Resource
    private TokenService tokenService;

    /**
     * 查询任务列表列表
     */
    @GetMapping("/list")
    @ApiOperation("查询任务列表列表")
    @DynamicResponseParameters(properties = {
	        @DynamicParameter(name = "total", value = "总记录数"),
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "rows", value = "返回业务数据（数组类型）", dataTypeClass = Tasklist.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    public TableDataInfo list(Tasklist tasklist)
    {
        startPage();
        List<Tasklist> list = tasklistService.selectTasklistList(tasklist);
        return getDataTable(list);
    }

    /**
     * 导出任务列表列表
     */
    @ApiOperation("导出任务列表列表")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "文件名")
    })
    @PreAuthorize("@ss.hasRole('teacher')")
    @Log(title = "任务列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Tasklist tasklist)
    {
        List<Tasklist> list = tasklistService.selectTasklistList(tasklist);
        ExcelUtil<Tasklist> util = new ExcelUtil<Tasklist>(Tasklist.class);
        return util.exportExcel(list, "task");
    }

    /**
     * 获取任务列表详细信息
     */
    @ApiOperation("获取任务列表详细信息")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "data", value = "返回业务数据", dataTypeClass = Tasklist.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tasklistService.selectTasklistById(id));
    }

    /**
     * 新增任务列表
     */
    @ApiOperation("新增任务列表")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasRole('teacher')")
    @Log(title = "任务列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tasklist tasklist)
    {
        return toAjax(tasklistService.insertTasklist(tasklist));
    }

    /**
     * 修改任务列表
     */
    @ApiOperation("修改任务列表")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasRole('teacher')")
    @Log(title = "任务列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tasklist tasklist)
    {
        return toAjax(tasklistService.updateTasklist(tasklist));
    }

    /**
     * 删除任务列表
     */
    @ApiOperation("删除任务列表")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    @PreAuthorize("@ss.hasRole('teacher')")
    @Log(title = "任务列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tasklistService.deleteTasklistByIds(ids));
    }

    /**
     * 获取个人任务列表
     */
    @PreAuthorize("@ss.hasAnyRoles('teacher,student')")
    @ApiOperation("获取个人任务列表")
    @GetMapping("/listMe")
    @DynamicResponseParameters(properties = {
            @DynamicParameter(name = "total", value = "总记录数"),
            @DynamicParameter(name = "code", value = "状态码，200正确，其他错误"),
            @DynamicParameter(name = "rows", value = "返回业务数据（数组类型）", dataTypeClass = Tasklist.class),
            @DynamicParameter(name = "msg", value = "返回消息内容")
    })
    public TableDataInfo listMe(HttpServletRequest request, Tasklist tasklist) {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long deptId = loginUser.getUser().getDeptId();
        return getDataTable(tasklistService.listMe(deptId, tasklist));
    }

}
